package com.js.nickcar.dialog;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;

import com.js.nickcar.entity.remote.OptionBean;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by LZY on 2017/9/29.
 */

public class DialogHelper {

    public static DatePickerDialog openDatePickerDialog(Context context, Date date, DatePickerDialog.OnDateSetListener onDateSetListener) {

        //直接创建一个DatePickerDialog对话框实例，并显示出来
        Calendar calendar = Calendar.getInstance();

        if (date != null) {
            calendar.setTime(date);
        }

        DatePickerDialog datePickerDialog = new DatePickerDialog(context, onDateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
        datePickerDialog.getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        );
        return datePickerDialog;
    }

    /**
     * 打开地区选择对话框
     */
    public static RegionDialog openRegionDialog(Context context, RegionDialog.OnRegionListener onRegionListener, String[] defaultRegion) {
        RegionDialog regionDialog = new RegionDialog(context, defaultRegion);
        regionDialog.setCancelable(true);
        regionDialog.setCanceledOnTouchOutside(true);
        regionDialog.setOnRegionListener(onRegionListener);
        regionDialog.show();
        return regionDialog;
    }

    public static OptionDialog openOptionDialog(Context context, ArrayList<OptionBean> options, OptionBean curOption, OptionDialog.OnConfirmListener confirmListener) {
        OptionDialog optionDialog = new OptionDialog(context)
                .setSpanCount(4)
                .setData(options, curOption)
                .setOnConfirmListener(confirmListener);
        optionDialog.show();
        return optionDialog;
    }

    public static AlertDialog.Builder showConfirmDialog(Context context, String message, DialogInterface.OnClickListener onClickListener) {
        AlertDialog.Builder builder = getDialog(context);
        builder.setMessage(message);
        builder.setPositiveButton("确定", onClickListener);
        builder.setNegativeButton("取消", null);
        return builder;
    }

    public static EditDialog openEditDialog(Context context, String title, String content, String hint, int type, EditDialog.OnConfirmListener confirmListener) {
        EditDialog editDialog = new EditDialog(context)
                .setTitle(title)
                .setContent(content)
                .setHint(hint)
                .setInputType(type)
                .setOnConfirmListener(confirmListener);
        editDialog.show();
        return editDialog;
    }

    /***
     * 获取一个dialog
     *
     * @param context
     * @return
     */
    public static AlertDialog.Builder getDialog(Context context) {
        return new AlertDialog.Builder(context);
    }

    public static void closeDialog(Dialog dialog) {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
}
